#!/sbin/openrc-run
description="A tool for service discovery, monitoring and configuration"
description_checkconfig="Verify configuration files"
description_healthcheck="Check health status"
description_reload="Reload configuration"

extra_commands="checkconfig"
extra_started_commands="healthcheck reload"

command="/usr/sbin/$RC_SVCNAME"
command_args="$consul_opts"
command_user="$RC_SVCNAME:$RC_SVCNAME"

supervisor=supervise-daemon
pidfile="/run/$RC_SVCNAME.pid"
output_log="/var/log/$RC_SVCNAME.log"
error_log="/var/log/$RC_SVCNAME.log"
umask=027
respawn_max=0
respawn_delay=10
healthcheck_timer=60

depend() {
	need net
	after firewall
}

checkconfig() {
	ebegin "Checking /etc/consul"
	$command validate /etc/consul
	eend $?
}

start_pre() {
	checkconfig
	checkpath -f -m 0640 -o "$command_user" "$output_log" "$error_log"
}

healthcheck() {
	$command config list -kind proxy-defaults
}

reload() {
	start_pre \
		&& ebegin "Reloading $RC_SVCNAME configuration" \
		&& supervise-daemon "$RC_SVCNAME" --signal HUP --pidfile "$pidfile"
	eend $?
}
